import "./lib/mui/css/mui.min.css"
import Vue from "vue"
import app_vue from "./app.vue"
import VueRouter from "vue-router"
import routes from "./router.js"
import 'mint-ui/lib/style.css';
import { Header,Field,Button, Swipe,SwipeItem,Toast,Loadmore } from 'mint-ui';
import VueResource from 'vue-resource'
import "./lib/custIcon/homeIcon/iconfont.css"
import "./lib/custIcon/star/iconfont.css"
import resturantListComponet from "./component/resturant/resturantllist.vue"
import numSelectCompont from "./component/common/numSelect.vue"
                                          


Vue.use(VueResource)
Vue.use(VueRouter);
Vue.component(Header.name, Header);
Vue.component(Field.name, Field);
Vue.component(Button.name, Button);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Loadmore.name, Loadmore);
Vue.component("resturant-list",resturantListComponet)
Vue.component("num-select",numSelectCompont)

var router = new VueRouter({
    routes,
    linkActiveClass:"mui-active",
    // scrollBehavior (to, from, savedPosition) {
    //     debugger
    //     return { x: 0, y: 100 }
    //   }
});



let hostName = window.location.hostname;
Vue.http.options.root = "//"+hostName+":9887";
//Vue.http.headers.common['Access-Control-Allow-Origin'] = '127.0.0.1:9000';
Vue.http.headers.common['Content-Type'] = "application/x-www-form-urlencoded";
Vue.http.headers.common['crossDomain'] = "true";
Vue.http.options.xhr = { withCredentials: true }

Vue.http.options.emulateJSON = true;
Vue.http.interceptors.push(function (request, next) {
        request.credentials = true;
        request.headers.map["Access-Control-Allow-Origin"] = hostName+':9000';
    　　next(function(response) {
         let body = response.body
         //登录验证
         let isLogin = body["notLogin"];
         if(isLogin){
            Vue.prototype.loginUser = null;
            this.$router.push({path:"/view/login",query: { type: 'notLogin'}})
            return {}
         }
        
        //判断后端是否异常，报错
         if(!response.ok){
            Toast("接口数据异常");
            return null;
         }
        
        return response;   
      });
});

router.beforeEach(function(to, from, next){
    if(to.meta.notlogin){
        next();
    }else{
        let isUser = Vue.prototype.loginUser;
        if(isUser != null){
            next();
            return;
        }

        let app =router.app;
        app.$http.get("user/isLogin").then(res=>{
            if(!res.ok){
                return
            }
            let data = res.body;
            if(data.success){
                let user = data.data;
                Vue.prototype.loginUser = user
                next();
            }else{
                next({path:"/view/login",query: { type: 'notLogin'}});
            }
        });
    }
    next()
});



var app = new Vue({
    el:".app",
    render:c=>c(app_vue),
    router:router,
});



function getCookie(name) 
{ 
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]); 
    else 
        return null; 
} 